﻿/*
创建人：longb
日期：2022-06-06
描述：生产开工--收料确认js
*/
var page = {
    workflowstepid: getParam("id"),
    ContainerNo: getParam("ContainerNo"),
    ContainerID: getParam("ContainerID"), //任务单ID //add by longb 2023-01-16
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSSendRequestHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        page.GetSpecInfo();
        page.GetBigSmallData();
        page.addEditCell();
    },
    addEditCell: function () //给easyuigrid新增editcell方法
    {
        $.extend($.fn.datagrid.methods, {
            editCell: function (jq, param) {
                return jq.each(function () {
                    var opts = $(this).datagrid('options');
                    var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
                    for (var i = 0; i < fields.length; i++) {
                        var col = $(this).datagrid('getColumnOption', fields[i]);
                        col.editor1 = col.editor;
                        if (fields[i] != param.field) {
                            col.editor = null;
                        }
                    }
                    $(this).datagrid('beginEdit', param.index);
                    for (var i = 0; i < fields.length; i++) {
                        var col = $(this).datagrid('getColumnOption', fields[i]);
                        col.editor = col.editor1;
                    }
                });
            }
        });
    },
    endEditing: function (gridId) {
        if (page.editIndex == undefined) { return true }
        if ($('#' + gridId).datagrid('validateRow', page.editIndex)) {
            $('#' + gridId).datagrid('endEdit', page.editIndex);
            page.editIndex = undefined;
            return true;
        } else {
            return false;
        }
    },
    //页面事件绑定
    bind: function () {
        //去封信息
        $('#BigTabel').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onClickCell: function (index, field, value) {
                //编辑单元格
                if (page.endEditing('BigTabel')) {
                    if (value > 0) {
                        $('#BigTabel').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                }
            },
            columns: [[
		            { field: 'PRODUCTNAME', title: '大件件号', width: 100, align: 'center' },
		            { field: 'PRODUCTDESC', title: '件号名称', width: 100 },
                    { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 100, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: '批次号', width: 100 },
                    { field: 'SENDQTY', title: '本次配送数量', width: 100, align: 'center' },
                    { field: 'EDITORQTY', title: '未配送数量', width: 100, align: 'center' },
                    { field: 'NNORECEIVEQTY', title: '本次收料数量', width: 100, align: 'center', editor: 'numberbox', styler: function () { return 'background-color:#f8f8f8;color:red'; }, formatter: function (value, row, index) { if (value < 0) { return 0; } else { return value; } } },
                    { field: 'NORECEIVEQTY', title: '未收料数量', width: 100, align: 'center' }
                ]]
        });
        //集件信息
        $('#SmallTabel').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onClickCell: function (index, field, value) {
                //编辑单元格
                if (page.endEditing('SmallTabel')) {
                    if (value > 0) {
                        $('#SmallTabel').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                }
            },
            columns: [[
		            { field: 'PRODUCTNAME', title: '小件件号', width: 100, align: 'center' },
		            { field: 'PRODUCTDESC', title: '件号名称', width: 100 },
                    { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 100, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: '批次号', width: 100 },
                    { field: 'SENDQTY', title: '本次配送数量', width: 100, align: 'center' },
                    { field: 'EDITORQTY', title: '未配送数量', width: 100, align: 'center' },
                    { field: 'NNORECEIVEQTY', title: '本次收料数量', width: 100, align: 'center', editor: 'numberbox', styler: function () { return 'background-color:#f8f8f8;color:red'; }, formatter: function (value, row, index) { if (value < 0) { return 0; } else { return value; } } },
                    { field: 'NORECEIVEQTY', title: '未收料数量', width: 100, align: 'center' }
                ]]
        });
        $('#ToolTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onClickCell: function (index, field, value) {
                //编辑单元格
                if (page.endEditing('ToolTable')) {
                    if (value > 0) {
                        $('#ToolTable').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                }
            },
            columns: [[
		            { field: 'NAME', title: '工装工具图号', width: 100, align: 'center' },
		            { field: 'DESCRIPTION', title: '工装工具名称', width: 100 },
                    { field: 'SPECIFICATION', title: '型号规格', width: 100 },
                    { field: 'RESOURCETYPENAME', title: '工装工具类型', width: 100 },
                    { field: 'QTY', title: '需求数量', width: 100, align: 'center' },
                    { field: 'CONFIRMQTY', title: '本次配送数量', width: 100, align: 'center' },
                    { field: 'NOCONFIRMQTY', title: '未配送数量', width: 100, align: 'center', formatter: function (value, row, index) { return row.QTY - row.CONFIRMQTY; } },
                    { field: 'NNORECEIVEQTY', title: '本次收料数量', width: 100, align: 'center', editor: 'numberbox', styler: function () { return 'background-color:#f8f8f8;color:red'; }, formatter: function (value, row, index) { if (value < 0) { return 0; } else { return value; } } },
                    { field: 'NNORECEIVEQTY1', title: '未收料数量', width: 100, align: 'center', formatter: function (value, row, index) { return row.CONFIRMQTY - row.RECEIVEQTY; } }
                ]]
        });
        $('#MaterialTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            onClickCell: function (index, field, value) {
                //编辑单元格
                if (page.endEditing('MaterialTable')) {
                    if (value > 0) {
                        $('#MaterialTable').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                }
            },
            columns: [[
		            { field: 'NAME', title: '物料编码', width: 100, align: 'center' },
		            { field: 'DESCRIPTION', title: '物料描述', width: 100 },
                    { field: 'SPECIFICATION', title: '型号规格', width: 100 },
                    { field: 'RESOURCETYPENAME', title: '材料类型', width: 100 },
                    { field: 'QTY', title: '需求数量', width: 100, align: 'center' },
                    { field: 'CONFIRMQTY', title: '本次配送数量', width: 100, align: 'center' },
                    { field: 'NOCONFIRMQTY', title: '未配送数量', width: 100, align: 'center', formatter: function (value, row, index) { return row.QTY - row.CONFIRMQTY; } },
                    { field: 'NNORECEIVEQTY', title: '本次收料数量', width: 100, align: 'center', editor: 'numberbox', styler: function () { return 'background-color:#f8f8f8;color:red'; }, formatter: function (value, row, index) { if (value < 0) { return 0; } else { return value; } } },
                    { field: 'NNORECEIVEQTY1', title: '未收料数量', width: 100, align: 'center', formatter: function (value, row, index) { return row.CONFIRMQTY - row.RECEIVEQTY; } }
                ]]
        });
    },
    //查询工序信息
    GetSpecInfo: function () {
        var param = {
            action: "GetSpecInfo"
        };
        page.post(param, function (res) {
            $('#OrderForm').form('load', res.ReturnData)
            if (!res.Result) {
                //displayMessage(res.Message,res.Result);
            }
        });
    },
    //查询大小件信息
    GetBigSmallData: function () {
        var param = {
            action: "GetBigSmallConfirmData",
            workflowstepid: page.workflowstepid,
            ContainerNo: page.ContainerNo,
            ContainerID: page.ContainerID //add by longb 2023-01-16
        };
        page.post(param, function (res) {
            $('#BigTabel').datagrid('loadData', res.DBTable);
            $('#SmallTabel').datagrid('loadData', res.ReturnData);
            $('#ToolTable').datagrid('loadData', res.DBDataSet.Table2);
            $('#MaterialTable').datagrid('loadData', res.DBDataSet.Table1);
        });
    },
    //大小件配送--收料确认
    ReceiveBigSmall: function (index) {
        var rows = $('#BigTabel').datagrid('getRows');
        if (index == 1) {
            rows = $('#SmallTabel').datagrid('getRows');
        }
        var data = [];
        $.each(rows, function (index, item) {
            if (item.NNORECEIVEQTY > 0) {
                data.push(item);
            }
        });
        if (data.length == 0) {
            displayMessage("订单所需物料已收料完");
            return;
        }
        var param = {
            action: "ReceiveConfirmBigSmall",
            data: JSON.stringify(data),
            workflowstepid: page.workflowstepid,
            ContainerID:page.ContainerID,
            ContainerNo: page.ContainerNo
        };
        page.post(param, function (res) {
            if (res.Result) {
                page.GetBigSmallData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //工装工具、辅材--收料确认
    RTReceive: function (index) {
        var rows = $('#ToolTable').datagrid('getRows');
        if (index == 2) {
            rows = $('#MaterialTable').datagrid('getRows');
        }
        var data = [];
        $.each(rows, function (index, item) {
            if (item.NNORECEIVEQTY > 0) {
                data.push(item);
            }
        });
        if (data.length == 0) {
            displayMessage("订单所需" + (index == 2 ? "辅材" : "工装工具") + "已收料完");
            return;
        }
        var param = {
            action: "ReceiveToolsTransport",
            data: JSON.stringify(data),
            TransportType: index,
            workflowstepid: page.workflowstepid,
            ContainerID: page.ContainerID,
            ContainerNo: page.ContainerNo
        };
        page.post(param, function (res) {
            if (res.Result) {
                page.GetBigSmallData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});